<?php

namespace App\Models\Schedule;

use App\Models\BaseModel;
use App\Models\Traits\KfSoftDeletes;
use App\Models\Traits\KfUid;

class ScheduleRuleModel extends BaseModel
{
    use KfUid;
    use KfSoftDeletes;

    protected $table = "schedule_rules";

    protected $fillable = [
        'shopId', 'name', 'start', 'end', 'duration', 'color',
    ];

    public static function getInfoByShopId(int $shopId, int $id)
    {
        return self::query()
            ->where('shopId', $shopId)
            ->where('id', $id)
            ->first();
    }

    public static function getCountByShopId(int $shopId, string $name = '', int $id = 0): int
    {
        return self::query()
            ->where('shopId', $shopId)
            ->when(
                $name, function ($query) use ($name) {
                $query->where('name', $name);
            }
            )
            ->when(
                $id, function ($query) use ($id) {
                $query->whereNotIn('id', [$id]);
            }
            )
            ->count();
    }
}
